#include <algorithm>
using namespace std;
#include <vector>
#include <queue>

struct TreeNode
{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    explicit TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

class Solution
{
public:
    static vector<int> largestValues(TreeNode* root)
    {
        vector<int> rv;
        if(root == nullptr)
            return rv;
        queue<TreeNode*> q;
        q.push(root);
        while(!q.empty())
        {
            int sz = q.size();
            int m = INT_MIN;//m不能是0,如果碰到负数结点就糟了
            while(sz--)
            {
                auto t = q.front();
                m = max(m, t->val);
                q.pop();
                if(t->left)     q.push(t->left);
                if(t->right)    q.push(t->right);
            }
            rv.push_back(m);
        }
        return rv;
    }
};